package com.zl.app.service.imp;

import com.zl.app.mapper.AppUserMapper;
import com.zl.app.model.AppUserModel;
import com.zl.app.service.AppUserService;
import com.zl.common.core.domain.AjaxResult;
import com.zl.common.core.domain.entity.SysUser;
import com.zl.common.utils.SecurityUtils;
import com.zl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppUserServiceImp implements AppUserService {

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private SysUserMapper userMapper;

    /**
     * App注册账号
     *
     * @param appUserModel
     * @return
     */
    @Override
    public int insertAppUser(AppUserModel appUserModel) {
        appUserModel.setCreateBy(appUserModel.getUserName());
        appUserModel.setPassword(SecurityUtils.encryptPassword(appUserModel.getPassword()));
        return appUserMapper.insertAppUser(appUserModel);
    }

    /**
     * 找回密码
     *
     * @param userName
     * @param password
     * @return
     */
    @Override
    public AjaxResult resetPwd(String userName, String password) {
        SysUser u = userMapper.selectUserByUserName(userName);
        if (u == null) {
            return AjaxResult.error("账号不存在，请重新输入！");
        }else {
            return AjaxResult.success(userMapper.resetUserPwd(userName, SecurityUtils.encryptPassword(password)));
        }

    }

    /**
     * App修改资料
     *
     * @param appUserModel
     * @return
     */
    @Override
    public int updateAppUser(AppUserModel appUserModel) {
        appUserModel.setUpdateBy(appUserModel.getUserName());
        return appUserMapper.updateAppUser(appUserModel);
    }
}
